
function g2geojson() { };

g2geojson.prototype.getOverlayGeoJson = function (overlay) {
    if (!overlay)
        return '';

    if (this.isArray(overlay)) {
        var result = [];
        var len = overlay.length;
        for (var i = 0; i < len; i++)
            result.push(this.getOverlayGeoJson(overlay[i]));
        return {'type' : 'GeometryCollection', 'geometries' : result};
    }

    if (overlay instanceof google.maps.Marker)
        return this.getPointGeoJson(overlay.getPosition());
    else if (overlay instanceof google.maps.Polyline)
        return this.getLineGeoJson(overlay.getPath());
    else if (overlay instanceof google.maps.Polygon)
        return this.getPolygonGeoJson(overlay.getPaths());

    throw 'Invalid argument specified';
};

g2geojson.prototype.getLineGeoJson = function (path) {
    if (!path)
        return null;
	return {'type' : 'LineString', 'coordinates' : this.getPathGeoJson.apply(this, arguments)}
};

g2geojson.prototype.getPolygonGeoJson = function (rings) {
    if (!rings)
        return null;

    var result = [];
    if (this.isArray(rings)) {
        var len = rings.length;
        for (var i = 0; i < len; i++)
			result.push(this.getPathGeoJson(rings[i]));
        return {'type' : 'Polygon', 'coordinates' : result};
    }

    if (rings instanceof google.maps.MVCArray) {
        var len = rings.getLength();
        for (var i = 0; i < len; i++)
			result.push(this.getPathGeoJson(rings.getAt(i)));
        return {'type' : 'Polygon', 'coordinates' : result};
    }

    throw 'Invalid argument specified';
};

g2geojson.prototype.getPointGeoJson = function (latlng) {
    if (!latlng)
        return null;
	return {'type' : 'Point', 'coordinates' : this.getCoordinateGeoJson.apply(this, arguments)}
};

g2geojson.prototype.getPathGeoJson = function (path) {
    if (!path)
        return null;

    var coordinates = [];
	//handle plain js array argument
    if (this.isArray(path)) {
        var len = path.length;
        for (var i = 0; i < len; i++)
            coordinates.push(this.getCoordinateGeoJson(path[i]));
        return coordinates;
    }
	
	//handle google MVCArray argument
    if (path instanceof google.maps.MVCArray) {
        var len = path.getLength();
        for (var i = 0; i < len; i++)
			coordinates.push(this.getCoordinateGeoJson(path.getAt(i)));
        return coordinates;
    }

	//throw exception if neither js array or google MVCArray was supplied
    throw 'Invalid argument specified'
};

g2geojson.prototype.getCoordinateGeoJson = function (latlng) {
    if (!latlng)
        return null;

    var lat = 0, lng = 0, alt = 0;
    if (arguments.length === 1) {
		//if the single argument supplied was an array, it should be an array of x, y, z coordinates
		//if the array contains more than 3 items in the array, throw an exception since only x, y, z are supported
        if (this.isArray(latlng)) {
            if (!(latlng.length === 2 || latlng.length === 3))
                throw 'The argument array should only contain 2 or 3 items corresponding to the "latitude", "longitude" and the optional "altitude" values';
            lat = latlng[0];
            lng = latlng[1];
            if (latlng.length === 3)
                alt = latlng[2];
        }
		//if the single argument supplied was an instance of the google LatLng class
        else if (latlng instanceof google.maps.LatLng) {
            lat = latlng.lat();
            lng = latlng.lng();
        }
        else
            throw 'Invalid argument specified'
    }
	//if 2 or 3 arguments were specified, then the user supplied (x, y) or (x, y, z) as the parameters to the method
    else if (arguments.length === 2 || arguments.length === 3) {
        lat = arguments[0];
        lng = arguments[1];
        if (arguments.length === 3)
            alt = arguments[2];
    }
	//throw an exception if more than 3 arguments were supplied
    else
        throw 'Too many arguments specified'

    var result = [lng, lat];
    if (alt != 0)
        result.push(alt);
    return result;
};

g2geojson.prototype.isArray = function (obj) {
    return obj.constructor == Array;
};